package mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {

	private static String host = "smtp.gmail.com";
	private static String socketfactoryPort = "465";
	private static String socketFactoryClass = "javax.net.ssl.SSLSocketFactory";
	private static String mailSmtpAuth = "true";
	private static String mailSmtpPort = "465";
	private static String login= "belfortair@gmail.com";
	private static String pass = "belfortair1";
	
    public static void sendEmailSSL(String to, String subject, String content) throws AddressException, MessagingException {
        // smtp properties
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.socketFactory.port", socketfactoryPort);
        props.put("mail.smtp.socketFactory.class",socketFactoryClass);
        props.put("mail.smtp.auth", mailSmtpAuth);
        props.put("mail.smtp.port", mailSmtpPort);
        
        // authentification
        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(login,pass);
                }
            }
        );
        
        // construct message
        Message message = new MimeMessage(session);
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        message.setContent(content, "text/html; charset=ISO-8859-1");
        // send email
        Transport.send(message);
        
    }
}